
# 实现对haproxy配置文件的 增删改查


def search_node(node=None):
    '''
    查询对应的节点内容
    '''
    if not node:
        node = input('输入你要查询的节点: ')
    with open('haproxy.conf', 'r', encoding='utf-8') as f:
        for line in f:
            node_line = line.strip().split()
            if len(node_line) == 2 and node_line[0] == 'backend' and node_line[1] == node:
                print(line, end='')
                for line in f:
                    node_line = line.strip().split()
                    if len(node_line) == 2 and node_line[0] == 'backend':
                        break
                    print(line, end='')
                break
        else:
            print('not found this node')


def del_node():
    '''
    删除对应节点
    '''
    pass


def add_node():
    '''
    添加对应节点
    '''
    pass


def mod_node():
    '''
    修改对应的节点
    '''
    pass


def main():
    tools = {'search': search_node, 'delete': del_node,
             'add': add_node, 'modify': mod_node}
    while True:
        print(' haproxy tools '.center(50, '-'))
        for key in tools:
            print(key)
        choice = input('输入你的选择或者[q]退出: ')
        if choice in tools:
            tools[choice]()
        elif choice == 'q':
            exit()
        else:
            pass


main()
